package com.dz.usersystem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dz.usersystem.entity.FilmUser;
import com.dz.usersystem.service.FilmUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;


/**用户的状态
 * 0.未登录
 * 1.登录
 * 2.注销
 * 3.挂失
 *
 */

/**
 * (FilmUser)表控制层
 *
 * @author makejava
 * @since 2020-12-17 16:43:04
 */
@RestController
@RequestMapping("user")
@Slf4j
@Transactional
public class FilmUserController {
    /**
     * 服务对象
     */
    @Resource
    private FilmUserService filmUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public FilmUser selectOne(String id) {
        return this.filmUserService.queryById(id);
    }

    @GetMapping("selectAll")
    public List<FilmUser> selectOne( ) {
        FilmUser user =new FilmUser();
        return filmUserService.queryAll(user);
    }

    //电影票购买
    @PostMapping(value = "filmGoumai")
    public String filmGouMai(@RequestBody String id) {
        FilmUser us = new FilmUser();
        JSONObject jsonObject = JSONObject.parseObject(id);
        Object id1 = jsonObject.get("id");
        Object count = jsonObject.get("count");
        Object name = jsonObject.get("name");
        log.info("---id1--"+id1);
        log.info("---积分--"+count);
        FilmUser user = filmUserService.queryById((String) id1);
        us.setId((String) id1);
        Date date =new  Date();
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        String str =  "时间:"+ simpleDateFormat.format(date)+" "+  user.getName() +"消费"+count+"积分购买了电影票<<"+(String) name+">>";
        String jilu = user.getJilu();
        if (jilu.length()==0){
            jilu=str;
        }else {
            jilu=jilu+","+str;
        }
        user.setJilu(jilu);
        FilmUser update01 = filmUserService.update(user);
        int oldCount=Integer.parseInt(user.getCount());
        int  newCount =oldCount-Integer.parseInt((String) count);
        if(newCount < 0){
            return "电影票购买失败";
        }else {
            us.setCount(String.valueOf(newCount));
            log.info("---电影票购买---" + (String)id1);
            FilmUser update = filmUserService.update(us);
            log.info("--电影票购买---" + update);
            if (!StringUtils.isEmpty(update)) {
                return "ok";
            } else {
                return "false";
            }
        }


    }

    //挂失
    @PostMapping(value = "guashi")
    public String guashi(@RequestBody String id) {
        FilmUser us = new FilmUser();
        JSONObject jsonObject = JSONObject.parseObject(id);
        Object id1 = jsonObject.get("id");
        log.info("---id1--"+id1);

        us.setId((String) id1);
        us.setState(3);
        log.info("---挂失id---" + (String)id1);
        FilmUser update = filmUserService.update(us);
        log.info("--挂失---" + update);
        if (!StringUtils.isEmpty(update)) {
            return "ok";
        } else {
            return "false";
        }
    }

    //注销
    @PostMapping(value = "zhuxiao")
    public String zhuxiao(@RequestBody String id) {
        FilmUser us = new FilmUser();
        JSONObject jsonObject = JSONObject.parseObject(id);
        Object id1 = jsonObject.get("id");
        log.info("---id1--"+id1);

        us.setId((String) id1);
        us.setState(2);
        log.info("---挂失id---" + (String)id1);
        FilmUser update = filmUserService.update(us);
        log.info("--挂失---" + update);
        if (!StringUtils.isEmpty(update)) {
            return "ok";
        } else {
            return "false";
        }
    }



    //退出
    @PostMapping(value = "tuichu")
    public String tuichu(@RequestBody String id){
        FilmUser us=new FilmUser();

        HashMap<String,String> map =new HashMap<>();
        String[] split = id.split("&");
        for (int i = 0; i <split.length ; i++) {
            String[] split1 = split[i].split("=");
            map.put(split1[0],split1[1]);
        }

        String ids = map.get("id");
        us.setId(ids);
        us.setState(0);
        log.info("---退出id---"+ids);
        FilmUser update = filmUserService.update(us);
        log.info("--退出---"+update);
        if(!StringUtils.isEmpty(update)){
            return "ok";
        }else {
            return "false";
        }

    }




    //更新
    @PostMapping(value = "updateUser")
    public FilmUser updateUser(@RequestBody String us) {
        try {
            us= URLDecoder.decode(us,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HashMap<String,String> map =new HashMap<>();
        FilmUser user =new FilmUser();
        String[] split = us.split("&");
        for (int i = 0; i <split.length ; i++) {
            String[] split1 = split[i].split("=");
            map.put(split1[0],split1[1]);
        }

        String name = map.get("name");
        String phone =map.get("phone");
        String password =map.get("password");
        String id =map.get("id");
        String username=map.get("username");
        log.info("-----updateMap:----"+map);
        user.setName(name);
        user.setId(id);
        user.setPhone(phone);
        user.setPassword(password);
        user.setUsername(username);


        log.info("-----user:------"+us);

        FilmUser update = filmUserService.update(user);

        return update;

    }
    //注册
    @PostMapping(value = "insertUser")
    public  String  insertUser(@RequestBody String us){
        FilmUser user =new FilmUser();


        HashMap<String,String> map =new HashMap<>();
        String[] split = us.split("&");
        for (int i = 0; i <split.length ; i++) {
            String[] split1 = split[i].split("=");
            map.put(split1[0],split1[1]);
        }

        String name = map.get("username");
        String phone =map.get("phone");
        String password =map.get("password");

        log.info("-----updateMap:----"+map);

        user.setUsername(name);
        List<FilmUser> filmUsers = filmUserService.queryAll(user);
        if (!StringUtils.isEmpty(filmUsers)){
            user.setPassword(password);
            user.setPhone(phone);
            String id = UUID.randomUUID().toString().replace("-", "");
            log.info("-----id---"+id);
            user.setId(id);
            user.setState(0);
            user.setCount("0");
            FilmUser insert = filmUserService.insert(user);
            log.info("-----增加成功---"+insert);
            return "新增成功";
        }
        else {
            return "新增失败,账号已存在";
        }




    }




}